package com.csii.mobilebanking.tool;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

import android.net.Proxy;

public class HttpUtils {

	public static class MyHttpClient {
		private static final DefaultHttpClient httpClient;

		public static HttpResponse executeHttpGet(HttpGet httpget,
				boolean wifiEnabled) throws ClientProtocolException,
				IOException {
			if (!wifiEnabled && needProxy()) {
				HttpHost httphost = getProxyHost();
				if (httphost != null
						&& !httphost.getHostName().equals("10.0.0.200")
						&& !httphost.getHostName().equals("010.000.000.200"))
					httpClient.getParams().setParameter(
							"http.route.default-proxy", httphost);
			}
			return httpClient.execute(httpget);
		}

		static {
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, true);
			HttpProtocolParams.setUserAgent(httpParams, "MeneameAndroid/0.1.6");
			ConnManagerParams.setMaxConnectionsPerRoute(httpParams,
					new ConnPerRouteBean(12));
			ConnManagerParams.setMaxTotalConnections(httpParams, 20);
			HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
			HttpConnectionParams.setSoTimeout(httpParams, 20000);
			HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
			HttpClientParams.setRedirecting(httpParams, false);
			SchemeRegistry schemeRegistry = new SchemeRegistry();
			schemeRegistry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(
					httpParams, schemeRegistry), httpParams);
		}
	}

	public static HttpHost getProxyHost() {
		if (Proxy.getDefaultHost() != null) {
			return new HttpHost(Proxy.getDefaultHost(), Proxy.getDefaultPort());
		} else {
			return null;
		}
	}

	public static boolean needProxy() {
		if (Proxy.getDefaultHost() == null)
			return false;
		else
			return true;
	}

	public static HttpClient getHttpClient() {
		BasicHttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 5000);
		HttpConnectionParams.setSoTimeout(params, 5000);
		DefaultHttpClient httpclient = new DefaultHttpClient(params);
		// if (needProxy()) {
		// HttpHost httphost = getProxyHost();
		// if (httphost != null)
		// httpclient.getParams().setParameter("http.route.default-proxy",httphost);
		// }
		return httpclient;
	}

	public static HttpEntity executeHttpGet(String url) {
		HttpClient httpclient = null;
		HttpGet httpget = null;
		HttpEntity httpentity = null;
		try {
			httpclient = getHttpClient();
			httpget = new HttpGet(url);

			HttpResponse httpresponse = httpclient.execute(httpget);
			if (httpresponse.getStatusLine().getStatusCode() == 200) {
				httpentity = httpresponse.getEntity();
				return httpentity;
			}
		} catch (IOException e) {
			if (httpget != null)
				httpget.abort();
			e.printStackTrace();
		}
		return null;
	}

	public static long getContentLength(String url) {
		long result = 0;
		HttpEntity httpentity = executeHttpGet(url);
		if (httpentity != null) {
			result = httpentity.getContentLength();
		}
		return result;
	}

	public static HttpEntity executeHttpPost(String url, String jsonstring) {
		HttpClient httpclient = null;
		HttpPost httppost = null;
		HttpEntity httpentity = null;
		try {
			httpclient = getHttpClient();
			httppost = new HttpPost(url);
			httppost.setEntity(new StringEntity((jsonstring), "UTF-8"));
			HttpResponse httpresponse = httpclient.execute(httppost);
			if (httpresponse.getStatusLine().getStatusCode() == 200) {
				httpentity = httpresponse.getEntity();
				return httpentity;
			}
		} catch (IOException e) {
			if (httppost != null)
				httppost.abort();
			e.printStackTrace();
		}
		return null;
	}

}
